package org.diysearch.tests;

import java.io.StringWriter;

import junit.framework.TestCase;

import org.diysearch.interfaces.Product;
import org.diysearch.interfaces.Service;
import org.diysearch.tools.BaseTool;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockJspWriter;

public class TestServices extends TestCase {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	StringWriter output = new StringWriter();
	MockJspWriter writer =  new MockJspWriter(output);
	
	public void testConfirmService() {
		String token = "cdceec13d9ef439832444055ac7596d5";
		Service svc = null;
		
		try {
			svc = (Service) BaseTool.IOC_ENGINE.getBean("confirm");
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("bean lookup fail");
		}
		
		request.addParameter("t",token);
		svc.setRequest(request);
		svc.setResponse(response);
		svc.setWriter(writer);
		
		try {
			Product p = svc.execute();
			assertNotNull("product not null",p);
			p.print();
			System.out.println(output.toString());
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("confirm fail");
		}
	}
	
	public void testRegisterService () {
		Service svc = null;
		try {
			svc = (Service) BaseTool.IOC_ENGINE.getBean("register");
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("bean lookup fail");
		}
		request.addParameter("email", "blinder.dave@gmail.com");
		request.addParameter("username","test-user_"+System.currentTimeMillis());
		request.addParameter("password", "test-password");
		
		svc.setRequest(request);
		svc.setResponse(response);
		svc.setWriter(writer);

		try {
			Product p = svc.execute();
			assertNotNull("product not null",p);
			p.print();
			System.out.println(output.toString());
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("register fail");
		}
	}
}
